﻿using System;
using INdTFramework.Http;
using RestServicesSample.Model;
using RestSharp;

namespace RestServicesSample.Services
{
    public class SampleApi
    {
        /// <summary>
        /// RestSharpWrapper instance to realize HTTP requests.
        /// 
        /// </summary>
        private RestSharpWrapper restClient = new RestSharpWrapper("http://blog.justkaws.com/api");

        /// <summary>
        /// Get the full list of users from our sample rest service. 
        /// 
        /// </summary>
        /// <param name="success"></param>
        /// <param name="fail"></param>
        public void GetUsers(Action<UserList> success, Action<String> fail)
        {
            DefaultJsonHttpRequest<UserList, string> request =
                new DefaultJsonHttpRequest<UserList, string>("/user", Method.GET, success, fail);
            
            restClient.ExecuteAsync(request);
        }

        /// <summary>
        /// Get info about user id informed by userId.
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="success"></param>
        /// <param name="fail"></param>
        public void GetUser(int userId, Action<User> success, Action<String> fail)
        {
            String resource = String.Format(Api.UserUri, userId);
            DefaultJsonHttpRequest<User, string> request =
                new DefaultJsonHttpRequest<User, string>(resource, Method.GET, success, fail);

            restClient.ExecuteAsync(request);
        }

        /// <summary>
        /// Include a new user on remote database.
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="success"></param>
        /// <param name="fail"></param>
        public void InsertUser(User user, Action<String> success, Action<String> fail)
        {
            DefaultJsonHttpRequest<String, String> request =
                new DefaultJsonHttpRequest<String, String>("/user", Method.POST, success, fail);

            string json = "{\"id\": \"5\",\"name\": \"neusa\",\"surname\": \"leelee\",\"email\": \"neusa@hell.com\",\"phone\": \"345246666\"}";

            request.AddBody(json);
            request.AddHeader("Content-Type", "application/json");
            restClient.ExecuteAsync(request);
        }

        /// <summary>
        /// Updates a existing user on remote database.
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="success"></param>
        /// <param name="fail"></param>
        public void UpdateUser(User user, Action<String> success, Action<String> fail)
        {
            DefaultJsonHttpRequest<String, String> request =
                new DefaultJsonHttpRequest<String, String>("/user", Method.PUT, success, fail);

            request.AddBody(user);
            request.AddHeader("Content-Type", "application/json");
            restClient.ExecuteAsync(request);
        }

        public void DeleteUser(int userId, Action<User> success, Action<String> fail)
        {
            String resource = String.Format(Api.UserUri, userId);

            DefaultJsonHttpRequest<User, String> request =
                new DefaultJsonHttpRequest<User, String>(resource, Method.DELETE, success, fail);

            restClient.ExecuteAsync(request);
        }
    }
}
